package com.guli.edu.controller;


import com.guli.commonutils.R;
import com.guli.edu.client.VodClient;
import com.guli.edu.entity.Video;
import com.guli.edu.service.VideoService;
import com.guli.servicebase.exceptionhandler.GuliException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程视频 前端控制器
 * </p>
 *
 * @author liuzg
 * @since 2021-03-08
 */
@RestController
@RequestMapping("/edu/video")
@Api(tags = "小节管理")
public class VideoController {

    @Autowired
    private VideoService videoService;

    @Autowired
    private VodClient vodClient;


    @ApiOperation(value = "添加小节")
    @PostMapping(value = "/addVideo")
    public R addVideo(@RequestBody Video eduVideo) {
        videoService.save(eduVideo);
        return R.ok();
    }


    // TODO 后面这个方法需要完善：删除小节时候，同时把里面视频删除
    @ApiOperation(value = "删除小节")
    @DeleteMapping(value = "/remove/{id}")
    public R deleteVideo(@PathVariable String id) {
        //根据小节id获取视频id，调用方法实现视频删除
        Video byId = videoService.getById(id);
        String videoSourceId = byId.getVideoSourceId();
        //判断小节里面是否有视频id
        if(!StringUtils.isEmpty(videoSourceId)) {
            R r = vodClient.removeAlyVideo(videoSourceId);
            if(r.getCode() == 20001) {
                throw new GuliException(20001,"删除视频失败，熔断器...");
            }
        }
        //删除小节
        videoService.removeById(id);
        return R.ok();
    }

    // TODO 修改小节

}

